package com.jc.algorithmes.binarytree;

/**
 * 二叉树的后序遍历：
 * 1.先遍历左右节点，再遍历根节点
 * 2.采用递归法实现
 */
public class PostOrderDemo {

    public static void main(String[] args) {
        TreeNode node7 = new TreeNode(7,null,null);
        TreeNode node6 = new TreeNode(6,null,null);
        TreeNode node5 = new TreeNode(5,node6,node7);
        TreeNode node4 = new TreeNode(4,null,null);
        TreeNode node3 = new TreeNode(3,null,null);
        TreeNode node2 = new TreeNode(2,node4,node5);
        TreeNode node1 = new TreeNode(1,node2,node3);

        postorder(node1);
    }

    public static void postorder(TreeNode root){
        if (root == null){
            return;
        }
        postorder(root.left);
        postorder(root.right);
        System.out.println(root.val);//后序遍历：第三次成为栈顶元素即打印
    }

}
